package com.xc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.common.ServerResponse;
import com.xc.dao.SiteLoginLogMapper;
import com.xc.pojo.SiteLoginLog;
import com.xc.pojo.User;
import com.xc.service.ISiteLoginLogService;
import com.xc.utils.ip.IpUtils;
import com.xc.utils.ip.JuheIpApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author mipha
 * @version 1.0
 * @date 2019/2/16 2:55 PM
 */
@Service("iSiteLoginLogService")
@Slf4j
public class SiteLoginLogServiceImpl implements ISiteLoginLogService {

    @Autowired
    SiteLoginLogMapper siteLoginLogMapper;


    @Override
    public ServerResponse saveLog(User user, HttpServletRequest request) {

        //  保存登陆日志
        SiteLoginLog siteLoginLog = new SiteLoginLog();
        siteLoginLog.setUserId(user.getId());
        siteLoginLog.setUserName(user.getRealName());
        String ips = IpUtils.getIp(request);
//            String ips = "116.232.36.152";
        siteLoginLog.setLoginIp(ips);
        String ip_address = JuheIpApi.ip2Add(ips);
        siteLoginLog.setLoginAddress(ip_address);
        siteLoginLog.setAddTime(new Date());
        siteLoginLogMapper.insert(siteLoginLog);

        return ServerResponse.createBySuccess();
    }


    @Override
    public ServerResponse<PageInfo> loginList(Integer userId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SiteLoginLog> siteLoginLogs = siteLoginLogMapper.loginList(userId);
        PageInfo pageInfo = new PageInfo(siteLoginLogs);
        return ServerResponse.createBySuccess(pageInfo);
    }



}
